var app = getApp();
Page({
  data: {
    baseUrl: app.d.ceshiUrl,

    currType:1,
    types: [],
    cartsList: [],
    total: 0,
    carts: [],
    contractName: null,
    productData: [],
  },

  onLoad: function(option) {
    var that = this;
    console.log(option)
    var id = option["id"]
    var types = []
    that.setData({
      currType:id,
      types: types,
    })
    var cartslist = that.data.cartsList;
    // console.log(that.data.carts)
    // console.log('getcarts')
    that.init(id);
    that.sum();
  },
  init: function(id) {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/goods/GoodsClassMgrAction!list',
      method: 'post',
      data: {},
      header: app.globalData.header,
      success: function(res) {
        var rows = res.data.rows;
        app.globalData.carts = rows;
        console.log(res);
        console.log("proList");
        that.setData({
          types: rows,
        });
        that.getType(id)
        that.getCat();
      },
      fail: function(e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },
  bindMinus: function(e) {
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index);
    var num = that.data.productData[index].count;

    var cartid = e.currentTarget.dataset.cartid;
    if (num > 1) {
      num--;
      e = {
        'cartid': cartid,
        'num': num,
        'index': index
      };
      that.updataNum(e);
    }
    if (num == 1) {
      e = {
        'cartid': cartid,
      };
      that.removeShopCard(e)
    }
    console.log(num);
  },
  bindPlus: function(e) {
    var that = this;
    console.log('bindPlus')
    console.log(e)
    var index = parseInt(e.currentTarget.dataset.index);
    var num = that.data.productData[index].count;
    var price = that.data.productData[index].goods_price;
    var goodsid = that.data.productData[index].id;
    var cartid = e.currentTarget.dataset.cartid;
    // console.log('num')
    // console.log(num)
    // console.log(price)
    // console.log(goodsid)
    // console.log(cartid)
    // console.log('num1')
    if (num == 0) {

      var a = {
        'goodsid': goodsid,
        'price': price,
      };
      that.addToCat(a)
    } else {
      num++;
      console.log(num);
      e = {
        'cartid': cartid,
        'num': num,
        'index': index
      };
      that.updataNum(e);
    }
  },

  sum: function() {
    var that = this;
    var carts = this.data.carts;
    // 计算总金额
    var total = 0;
    for (var i = 0; i < carts.length; i++) {
      if (carts[i].selected) {
        total += carts[i].count * carts[i].price;
      }
    }
    // 写回经点击修改后的数组
    that.setData({
      carts: carts,
      total: total
    });
  },

  // 添加购物车
  addToCat: function(e) {
    var goodsid = e.goodsid;
    var price = e.price;
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/transaction/StoreCartMgrAction!addGoods',
      method: 'get',
      data: {
        "goodsCart.goods.id": goodsid,
        'goodsCart.count': 1,
        'goodsCart.price': price,
      },
      header: app.globalData.header,
      success: function(res) {
        var data = res.data;
        console.log(res)
        console.log(res.data.msg);
        that.getCat();
        if (data.success == 'false') {
          wx.showToast({
            title: '请重新登录',
            duration: 2000
          });
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });
  },
  // 从购物车中删除
  removeShopCard: function(e) {
    var that = this;
    var cardId = e.cartid;
    wx.request({
      url: app.d.ceshiUrl + '/transaction/GoodsCartMgrAction!del',
      method: 'get',
      data: {
        'goodsCart.id': cardId,
      },
      header: app.globalData.header,
      success: function(res) {
        console.log(res)
        var data = res.data;
        if (data.success == 'true') {
          that.getCat()
        } else {
          wx.showToast({
            title: '操作失败！',
            duration: 2000
          });
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });
  },
  updataNum: function(e) {
    var that = this;
    var cartid = e.cartid;
    var num = e.num;
    var index = e.index;
    wx.request({
      url: app.d.ceshiUrl + '/transaction/GoodsCartMgrAction!update',
      method: 'get',
      data: {
        'goodsCart.id': cartid,
        'goodsCart.count': num,
      },
      header: app.globalData.header,
      success: function(res) {
        var status = res.data.success;
        console.log(res)
        if (status == 'true') {
          var minusStatus = num <= 1 ? 'disabled' : 'normal';
          var produces = that.data.productData;
          produces[index].count = num;
          that.setData({
            productData: produces
          });
          that.sum();
        } else {
          wx.showToast({
            title: '操作失败！',
            duration: 2000
          });
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });
  },
  //  选择分类
  tapType: function(e) {
    var that = this;
    const currType = e.currentTarget.dataset.typeId;
    that.getType(currType)
    // that.setData({
    //   productData: null
    // });
    that.setData({
      currType: currType
    });
    // console.log(currType);
    // console.log(that.data.productData);
  },

  getType: function(id) {
    var that = this;
    wx.request({
      // url: 'http://192.168.1.111:8080/ADPSys/goods/GoodsMgrAction!list',
      url: app.d.ceshiUrl +'/goods/GoodsMgrAction!list',
      method: 'get',
      data: {
        'goods.gc.id': id,
      },
      header: app.globalData.header,
      success: function(res) {
        var rows = res.data.rows;
        app.globalData.carts = rows;
        console.log(res);
        console.log(id);
        console.log("tapType");
        that.setData({
          productData: rows,
        });
        that.getCat();
      },
      fail: function(e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    });
  },
  //  获取购物车数据
  getCat: function() {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/transaction/StoreCartMgrAction!listGoods',
      method: 'post',
      header: app.globalData.header,
      success: function(res) {
        // console.log(res)
        var cart = res.data.rows;
        var prolist = that.data.productData
        var total = 0;
        // console.log('getCat')
        // console.log(res)
        if (res.data.success == 'false') {

          cart = ''
          that.setData({
            carts: [],
          });
        }
        if (cart == '') {
          for (let i = 0; i < prolist.length; i++) {
            prolist[i].count = 0;
          }
          that.setData({
            carts: [],
            productData: prolist,
          });
          return
        }

        for (let i = 0; i < prolist.length; i++) {
          for (let j = 0; j < cart.length; j++) {
            if (prolist[i].id == cart[j].goods_id) {
              prolist[i].count = cart[j].count;
              prolist[i].cartid = cart[j].id;
              break;
            } else {
              prolist[i].count = 0;
            }
          }
        }
        that.setData({
          productData: prolist,
          contractName: res.data.contract_name,
          carts: cart,
          total: total
        });
        // console.log(that.data.contractName);

      },
      fail: function(e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    });
  },

})